package ml;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;

import parser.ParserAnnotation;
import parser.ParserPipeline;
import corpus.WordPolarityCorpus;
import forum.ForumPost;
import forum.ForumSentence;

public class VoteFlip extends MajorityVoting {
	private TreeSet<String> reversals;

	public VoteFlip(String name, ParserPipeline pp, String[] labels,
			WordPolarityCorpus wpc) throws IOException {
		super(name, pp, labels, wpc);
		reversals = new TreeSet<String>();
		Scanner scr = new Scanner(new File("corpus/polwords/reversal.txt"));
		while (scr.hasNextLine())
			reversals.add(scr.nextLine().trim());
		scr.close();
	}
	
	@Override
	public void classify(ForumPost post) throws Exception {
		super.classify(post);
		ParserAnnotation pa = post.getParserAnnotation();
		for (int i = 0; i < pa.getNbrSentences(); i++) {
			if (post.getSentences().get(i).getEmotion().equals("neu"))
				continue;
			List<String> tokens = pa.getTokens(i);
			int reverseTokens = 0;
			for (String s : tokens)
				if (reversals.contains(s))
					reverseTokens++;
			if (reverseTokens % 2 != 0) {
				ForumSentence fs = post.getSentences().get(i);
				if (fs.getEmotion().equals("pos"))
					fs.setEmotion("neg");
				else if (fs.getEmotion().equals("neg"))
					fs.setEmotion("pos");
			}
		}
	}

}
